﻿using CSharpDay11.Model;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Data.Entity.Migrations;

namespace CSharpDay11.Data
{
    public class Seeder
    {
        private ApplicationUser _user2;
        private ApplicationUser _user11;
        private ApplicationUser _user12;
        private ApplicationUser _user13;
        private ApplicationUser _user14;
        private ApplicationUser _user15;

        public void Seed(ApplicationDbContext db, bool roles = false, bool users = false, bool artist = false, bool art = false)
        {
            var store = new UserStore<ApplicationUser>(db);
            var manager = new UserManager<ApplicationUser>(store);

            if (roles) SeedRoles(db);
            if (users) SeedUsers(db, manager);
            if (artist) SeedArtist(db);
            if (art) SeedArt(db);
        }
        private void SeedRoles(ApplicationDbContext context)
        {
            var store = new RoleStore<IdentityRole>(context);
            var manager = new RoleManager<IdentityRole>(store);

            if (!context.Roles.Any(x => x.Name == "Admin"))
            {
                var role = new IdentityRole() { Name = "Admin" };
                manager.Create(role);
            }
            if (!context.Roles.Any(x => x.Name == "Basic User"))
            {
                var role = new IdentityRole() { Name = "Basic User" };
                manager.Create(role);
            }
        }
        private void SeedUsers(ApplicationDbContext context, UserManager<ApplicationUser> manager)
        {
            if (!context.Users.Any(x => x.UserName == "monnet"))
            {
                _user11 = new ApplicationUser()
                {
                    UserName = "monnet",
                    Email = "test@test.com"
                };
                manager.Create(_user11, "123123");
                manager.AddToRole(_user11.Id, "Basic User");
            }
            else
            {
                _user11 = context.Users.Where(x => x.UserName == "monnet").FirstOrDefault();
            }
            if (!context.Users.Any(x => x.UserName == "picasso"))
            {
                _user12 = new ApplicationUser()
                {
                    UserName = "picasso",
                    Email = "test@test.com"
                };
                manager.Create(_user12, "123123");
                manager.AddToRole(_user12.Id, "Basic User");
            }
            else
            {
                _user12 = context.Users.Where(x => x.UserName == "picasso").FirstOrDefault();
            }
            if (!context.Users.Any(x => x.UserName == "dali"))
            {
                _user13 = new ApplicationUser()
                {
                    UserName = "dali",
                    Email = "test@test.com"
                };
                manager.Create(_user13, "123123");
                manager.AddToRole(_user13.Id, "Basic User");
            }
            else
            {
                _user13 = context.Users.Where(x => x.UserName == "dali").FirstOrDefault();
            }
            if (!context.Users.Any(x => x.UserName == "kahlo"))
            {
                _user14 = new ApplicationUser()
                {
                    UserName = "kahlo",
                    Email = "test@test.com"
                };
                manager.Create(_user14, "123123");
                manager.AddToRole(_user14.Id, "Basic User");
            }
            else
            {
                _user14 = context.Users.Where(x => x.UserName == "kahlo").FirstOrDefault();
            }
            if (!context.Users.Any(x => x.UserName == "vangogh"))
            {
                _user15 = new ApplicationUser()
                {
                    UserName = "vangogh",
                    Email = "test@test.com"
                };
                manager.Create(_user15, "123123");
                manager.AddToRole(_user15.Id, "Basic User");
            }
            else
            {
                _user15 = context.Users.Where(x => x.UserName == "vangogh").FirstOrDefault();
            }
            if (!context.Users.Any(x => x.UserName == "Admin"))
            {
                _user2 = new ApplicationUser()
                {
                    UserName = "Admin",
                    Email = "admin@admin.com",
                };
                manager.Create(_user2, "123123");
                manager.AddToRole(_user2.Id, "Admin");
            }
            else
            {
                _user2 = context.Users.Where(x => x.UserName == "Admin").FirstOrDefault();
            }

        }
        private void SeedArtist(ApplicationDbContext db)
        {
            db.Artists.AddOrUpdate(x => x.ArtistName,
                new Artist() { ArtistName = "Jean Claude Monnet", UserId = _user11.Id},
                new Artist() { ArtistName = "Pablo Picasso", UserId = _user12.Id},
                new Artist() { ArtistName = "Salvador Dali", UserId = _user13.Id},
                new Artist() { ArtistName = "Frida Kahlo", UserId = _user14.Id},
                new Artist() { ArtistName = "Van Gogh", UserId = _user15.Id});
            db.SaveChanges();
        }
        private void SeedArt(ApplicationDbContext db)
        {
            db.Artworks.AddOrUpdate(x => x.ArtName,
                new Art() { ArtName = "San Giorgio Maggiore Venice at twilight", ArtUrl = "http://www.intermonet.com/prints/m1768003.jpg", ArtDesc = "Saint-Georges Majeur au crépuscule, Claude MONET 1908National Museum of Wales, Cardiff, GB", ArtistId = 1},
                new Art() { ArtName = "Le Palais Da Mula", ArtUrl = "http://www.intermonet.com/prints/m1764040.jpg", ArtDesc = "Claude MONET 1908National Gallery of Art, Washington", ArtistId = 1 },
                new Art() { ArtName = "Venise, le Palais ducal", ArtUrl = "http://www.intermonet.com/prints/w1743t400.jpg", ArtDesc = "Claude MONET 1908Brooklyn Museum, New York", ArtistId = 1 },
                new Art() { ArtName = "Venise, le Grand Canal", ArtUrl = "http://www.intermonet.com/prints/w1737t400.jpg", ArtDesc = "Claude MONET 1908Private Collection", ArtistId = 1 },
                new Art() { ArtName = "Self-Portrait Picasso", ArtUrl = "http://uploads2.wikiart.org/images/pablo-picasso/self-portrait-1907.jpg!Blog.jpg", ArtDesc = "Self-Portrait", ArtistId = 2 },
                new Art() { ArtName = "The Picador", ArtUrl = "http://uploads0.wikiart.org/images/pablo-picasso/the-picador-1890.jpg!Blog.jpg", ArtDesc = "A man and his horse", ArtistId = 2 },
                new Art() { ArtName = "The old fisherman", ArtUrl = "http://uploads6.wikiart.org/images/pablo-picasso/the-old-fisherman-1895.jpg", ArtDesc = "The old fisherman", ArtistId = 2 },
                new Art() { ArtName = "Head of a Man in El Greco", ArtUrl = "http://uploads2.wikiart.org/images/pablo-picasso/head-of-a-man-in-el-greco-style-1899.jpg", ArtDesc = "Head of a Man in El Greco style", ArtistId = 2 },
                new Art() { ArtName = "First Communion", ArtUrl = "http://uploads4.wikiart.org/images/pablo-picasso/first-communion-1896.jpg", ArtDesc = "First Communion", ArtistId = 2 },
                new Art() { ArtName = "Study for the dream sequence in Spellbound", ArtUrl = "http://www.dali-gallery.com/images/works/1945_25.jpg", ArtDesc = "- Figueras, Fundación Gala-Salvador Dalí, Gift of Dalí to the Spanish State -", ArtistId = 3 },
                new Art() { ArtName = "Portrait of a Woman, 1945", ArtUrl = "http://www.dali-gallery.com/images/works/1945_20.jpg", ArtDesc = "Portrait of a Woman, 1945", ArtistId = 3 },
                new Art() { ArtName = "The Temptation of Saint Anthony, 1946", ArtUrl = "http://www.dali-gallery.com/images/works/1946_01.jpg", ArtDesc = "The Temptation of Saint Anthony, 1946", ArtistId = 3 },
                new Art() { ArtName = "Dematerialization Near the Nose of Nero, 1947", ArtUrl = "http://www.dali-gallery.com/images/thumb/_1947_01.jpg", ArtDesc = "Dematerialization Near the Nose of Nero, 1947", ArtistId = 3 },
                new Art() { ArtName = "La Turbie - Sir James Dunn Seated, 1949", ArtUrl = "http://www.dali-gallery.com/images/thumb/_1949_13.jpg", ArtDesc = "La Turbie - Sir James Dunn Seated, 1949", ArtistId = 3 },
                new Art() { ArtName = "Self-Portrait Kahlo", ArtUrl = "http://www.artchive.com/artchive/k/kahlo/kahlo_self26.jpg", ArtDesc = "1926 Oil on canvas 31 x 23 in Private collection, Mexico City", ArtistId = 4 },
                new Art() { ArtName = "Henry Ford Hospital", ArtUrl = "http://www.artchive.com/artchive/k/kahlo/thumb/kahlo_henry_ford.jpg", ArtDesc = "Collection Dolores Olmedo Foundation, Mexico City", ArtistId = 4 },
                new Art() { ArtName = "Diego Rivera", ArtUrl = "http://www.artchive.com/artchive/k/kahlo/thumb/kahlo_diego.jpg", ArtDesc = "Gelman Collection, Mexico City", ArtistId = 4 },
                new Art() { ArtName = "Self-Portrait as a Tehuana (Diego on My Mind)", ArtUrl = "http://www.artchive.com/artchive/k/kahlo/thumb/kahlo_tehuana.jpg", ArtDesc = "Self-Portrait as a Tehuana (Diego on My Mind)", ArtistId = 4 },
                new Art() { ArtName = "The Love Embrace of the Universe, the Earth (Mexico), Me, and Senor Xolotl", ArtUrl = "http://www.artchive.com/artchive/k/kahlo/thumb/kahlo_love_embrace.jpg", ArtDesc = "The Love Embrace of the Universe, the Earth (Mexico), Me, and Senor Xolotl", ArtistId = 4 },
                new Art() { ArtName = "Baby Marcelle Roulin", ArtUrl = "http://www.vangoghgallery.com/catalog/image/0441/Baby-Marcelle-Roulin,-The.jpg", ArtDesc = "Baby Marcelle Roulin", ArtistId = 5 },
                new Art() { ArtName = "Avenue of Plane Trees near Arles Station", ArtUrl = "http://www.vangoghgallery.com/catalog/image/0398/Avenue-of-Plane-Trees-near-Arles-Station.jpg", ArtDesc = "Avenue of Plane Trees near Arles Station", ArtistId = 5 },
                new Art() { ArtName = "Avenue of Poplars in Autumn", ArtUrl = "http://www.vangoghgallery.com/catalog/image/0122/Avenue-of-Poplars-in-Autumn.jpg", ArtDesc = "Avenue of Poplars in Autumn", ArtistId = 5 });
            db.SaveChanges();
        }
    }
}
